//
//  NRWCollectCarButton.m
//  NRWMall
//
//  Created by 张记 on 2018/6/8.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWCollectCarButton.h"
@interface NRWCollectCarButton ()
{
    UIImageView *_collectImageView;
    CAEmitterLayer *_effectLayer;
    CAEmitterCell *_effectCell;
    NRWUserInfoModel *_userModel;
}
@end

@implementation NRWCollectCarButton

-(instancetype)initWithFrame:(CGRect)frame collectImage:(UIImage *)collectImage unCollectImage:(UIImage *)unCollectImage{
    self=[super initWithFrame:frame];
    if (self) {
        _collectImage = collectImage;
        _unCollectImage = unCollectImage;
        _userModel = [NRWUserInfoModel shared];
        [self initBaseLayout];
    }
    return self;
}
-(void)setCollectImage:(UIImage *)collectImage{
    _collectImage = collectImage;
    _collectImageView.image = collectImage;
    
}
-(void)setUnCollectImageNO:(UIImage *)unCollectImageNO{
    _unCollectImage = unCollectImageNO;
}
-(void)setUnCollectImage:(UIImage *)unCollectImage{
    _unCollectImage = unCollectImage;
    _collectImageView.image = unCollectImage;
    
}
-(void)initBaseLayout{
    _isSelected = NO;
    _effectLayer=[CAEmitterLayer layer];
    [_effectLayer setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    [self.layer addSublayer:_effectLayer];
    [_effectLayer setEmitterShape:kCAEmitterLayerCircle];
    [_effectLayer setEmitterMode:kCAEmitterLayerOutline];
    [_effectLayer setEmitterPosition:CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2)];
    [_effectLayer setEmitterSize:CGSizeMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    _effectCell=[CAEmitterCell emitterCell];
    [_effectCell setName:@"zanShape"];
    [_effectCell setContents:(__bridge id)[UIImage imageNamed:@"EffectImage"].CGImage];
    [_effectCell setAlphaSpeed:-1.0f];
    [_effectCell setLifetime:1.0f];
    [_effectCell setBirthRate:0];
    [_effectCell setVelocity:50];
    [_effectCell setVelocityRange:50];
        [_effectLayer setEmitterCells:@[_effectCell]];
    _collectImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    [_collectImageView setImage:_unCollectImage];
    [_collectImageView setUserInteractionEnabled:YES];
    [self addSubview:_collectImageView];
    if (_collectImageView.gestureRecognizers == nil) {
        UITapGestureRecognizer *tapImageViewGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zanAnimationPlay)];
        [_collectImageView addGestureRecognizer:tapImageViewGesture];
    }

}
-(void)zanAnimationPlay{
    if (_userModel.isLogin == YES) {
        [self setIsSelected:!self.isSelected];
        if (self.clickHandler!=nil) {
            self.clickHandler(self);
        }
        [_collectImageView setBounds:CGRectMake(0, 0, 0, 0)];
        [UIView animateWithDuration:0.5f delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:5 options:UIViewAnimationOptionCurveLinear animations:^{
            [self->_collectImageView setBounds:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
            if (self.isSelected) {
                CABasicAnimation *effectLayerAnimation=[CABasicAnimation animationWithKeyPath:@"emitterCells.zanShape.birthRate"];
                [effectLayerAnimation setFromValue:[NSNumber numberWithFloat:100]];
                [effectLayerAnimation setToValue:[NSNumber numberWithFloat:0]];
                [effectLayerAnimation setDuration:0.0f];
                [effectLayerAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
                [self->_effectLayer addAnimation:effectLayerAnimation forKey:@"ZanCount"];
            }
        } completion:^(BOOL finished) {
        }];
    }else{

        NRWLoginView *loginView = [[NRWLoginView alloc] init];
        loginView.type = kCarDetailCollect;
        [loginView show];
    }
    
   
    
}
-(void)setIsSelected:(BOOL)isSelected{
    _isSelected=isSelected;
    if (isSelected) {
        [_collectImageView setImage:_collectImage];
    }else{
        [_collectImageView setImage:_unCollectImage];
    }
}

@end
